# 公用的工具
import functools
from flask import current_app
from flask import g
from flask import session
from info.models import User


def do_index_class(index):
    """这是一个过滤器"""
    if index == 0:
        return 'first'
    elif index == 1:
        return 'second'
    elif index == 2:
        return 'third'
    return ''

def user_login_data(f):
    @functools.wraps(f)
    def wrapper(*args,**kwargs):

        # 1. 登陆数据
        # 获取当前用户的数据
        user_id = session.get('user_id', None)
        user = None
        # 如果user_id存在就查询
        # 数据库
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                # 记录错误日志
                current_app.logger.error(e)
        g.user = user
        return f(*args,**kwargs)
    return wrapper